<?php 
    session_start();
?>

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        
        <link rel="stylesheet" href="styles/layout.css" type="text/css"/>
        <link rel="stylesheet" href="styles/computer.css" type="text/css"/>
        <script type="text/javascript" src="scripts/computer.js"></script>
        
        <script type="text/javascript">
            var xmlhttp;
            var userID;
            var i;
            function deleteUser(){
                var userIDRB = document.admin.userID;
                for(i = 0; i < userIDRB.length; i++){
                    if(userIDRB[i].checked){
                        userID = userIDRB[i].value;
                        break;
                    }
                }
//                alert(userID + " is clicked");
//                alert(userID);

                if(window.XMLHttpRequest){
                    //code for IE7+, Firefox, Chrome, and Safari
                    xmlhttp = new XMLHttpRequest();
                }else{
                    //code for IE6 and IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                
                xmlhttp.onreadystatechange = handleReply;
                xmlhttp.open("GET", "deleteUser.php?userID="+userID, true);
                xmlhttp.send();
            }
            
            function handleReply(){
                if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
//                    alert(xmlhttp.responseText);
                    userTable = document.getElementById("userTable");
                    userTable.deleteRow(i+1);
                }
            }
        </script>
        
    </head>
    <body>
        <?php        require 'header.html';
        ?>

        <div id="container">
  <div class="wrapper">
    <div id="content">
    	<div id="order">
        
        <?php
     
        if(isset($_SESSION["userID"]) && !strcmp($_SESSION["userType"], "administrator")){
        
        echo "Login: ".$_SESSION["userID"]." ".$_SESSION["userType"];
        echo "<br/>Welcome administrator";
        // put your code here
        $con = mysql_connect("localhost", "zhouzhao", "19831022");
        if(!$con){
            die("could not connect to DB: ".mysql_error());
        }
        
        mysql_select_db("cloudcom", $con);
        $result = mysql_query("select * from users");
        
        echo "<form method='post' name='admin' action='updateUser.php'>";
        echo "<table border='1' id='userTable'>
            <tr>
                <th>Username</th>
                <th>Password</th>
                <th>First name</th>
                <th>Last name</th>
                <th>DOB</th>
                <th>User type</th>
                <th>Select</th>
            </tr>";
        
        while($row = mysql_fetch_assoc($result)){
            echo "<tr>";
            echo "<td>".$row["username"]."</td>";
            echo "<td>".$row["password"]."</td>";
            echo "<td>".$row["firstName"]."</td>";
            echo "<td>".$row["lastName"]."</td>";
            echo "<td>".$row["dob"]."</td>";
            echo "<td>".$row["userType"]."</td>";
            echo "<td><input type='radio' name='userID' value='".$row["userID"]."'/></td>";
            echo "</tr>";
        }
        echo "</table>";
        ?>
        <input type="submit" value="Add or Change User" class="buttonStyle" onmouseover="changeOn(this)" onmouseout="changeOff(this)"/>
        <input type="button" value="Delete User" class="buttonStyle" onclick="deleteUser()" onmouseover="changeOn(this)" onmouseout="changeOff(this)"/>
        
        <?php
        echo "</form>";
        
        mysql_close($con);
        
        }else{
//            echo $_SESSION["userType"." ".$_SESSION["userID"]];
            require 'login.html';
        }
        ?>
            
    </div>	
    </div>
    <br class="clear" />
  </div>
</div>
        <?php        require 'tail.html';
        ?>
        
    </body>
</html>
